local ClientOHOH = _G.ClientOHOH
local M = ClientOHOH.class.new(script,ClientOHOH.files.OHOHOLYBUIBase())
local ConnectClass = _G.CommonOHOH.load.Connect()
local CallBackManager = ClientOHOH.load.CallBackManager()
local BtnHelper = ClientOHOH.load.BtnHelper()
local TweenService = game:GetService("TweenService")
--获取附属ui列表
local gSubUiList = {}
for _, vScript in pairs(script:GetChildren()) do
    if vScript:IsA("ModuleScript") then
        gSubUiList[vScript.Name] = require(vScript)
    end
end

function M.new(...)
    return M(...)
end

function Initsts(ui)
    ui.insts = {}
    ui.insts.root = "ctrl.insts.root"
    ui.insts.Close = "ctrl.insts.close"
    ui.insts.Scrolling = "ctrl.insts.scrolling"
end

function SubInitInsts(ui)
    for kName, vScript in pairs(gSubUiList) do
        ui.insts[kName] = vScript.GetInitUiList()
    end
end


function M.ctor(self)
    Initsts(self)
    SubInitInsts(self)
    M.super.ctor(self,ClientOHOH:GetPath_Loop("ReplicatedStorage.Resource.UI.ShopGui_new"))
    -- self:MobileSize()
    self.ShopBuyEvent = ConnectClass.new()
    
    for _, vScript in pairs(gSubUiList) do
        vScript.Init(self)
    end
	CallBackManager:AddRbxConnect(self,self.insts.Close.Activated,function()
		self:Hide()
	end)
  
    -- self:InitAllButtons()
end

function M.MobileSize(self)
    if _G.client.IsMobile then
        if self.insts.root then
            local pos = self.insts.root:GetAttribute("Position")
            local size = self.insts.root:GetAttribute("Size")

            if pos then
                self.insts.root.Position = pos
            end
            if size then
                self.insts.root.Size = size
            end
        end
    end    
end

--初始化所有按钮的基本属性
function M.InitAllButtons(self)
    self:AddProcess(function()
        for _, value in pairs(self.main_inst:GetDescendants()) do
            if value:IsA("GuiButton") then
                BtnHelper:InitButton(value,nil,nil,function() end)
            end
        end
    end)
end

function M.Go(self,tag)
    if not self:IsShowing() then
        self:Show()
    end
    local value = nil
    if tag == "wins" then
        value = 0.663
    else
        value = self.insts[tag].gSideTag.X.Value
    end
    self:AddProcess(function()
        self:Wait(0.1)
        self:ScrollingGo(value)
    end)
end

function M.ScrollingGo(self,targetX)
    local tweenInfo = TweenInfo.new(0.3,Enum.EasingStyle.Linear)
    local length = self.insts.Scrolling.AbsoluteCanvasSize.X
    local tw = TweenService:Create(self.insts.Scrolling,tweenInfo,{CanvasPosition = Vector2.new(length*targetX,0)})
    tw:Play()
end

function M.IsShowing(self)
    return self.main_inst.Enabled
end

function M.Full(self,key,...)
    assert(gSubUiList[key],"不存在UI元素"..key)
    gSubUiList[key].Full(...)
end

function M.Get(self,key,...)
    assert(gSubUiList[key],"不存在UI元素"..key)
    return gSubUiList[key].Get(...)
end

function M.dtor(self)
    M.super.dtor(self)
end


return M